layui.use(['form','layer','laydate'], function() {

    var $ = layui.$
        ,form = layui.form
        ,laydate = layui.laydate;


    //javascript扩展 对jquery的扩展
    $.extend({
        nano:function(template,data){//模板
            return template.replace(/\{([\w\.]*)\}/g, function(str, key) {
                var keys = key.split("."), value = data[keys.shift()];
                $.each(keys, function() {
                    value = value[this];
                });
                return (value === null || value === undefined) ? "" : value;
            });
        }
    });


    //去程航班日期
    laydate.render({
        elem: '#flight-date'
        ,type: 'date'
        ,range: ['#flight-start', '#flight-end']
        //,rangeLinked: true // 是否开启日期范围选择时的区间联动标注模式
        //,value: ['2022-05-01', '2022-06-01']
        //,value: '2022-05-01 - 2022-06-01'
    });

    //范围选择2
    laydate.render({
        elem: '#flight-date'
        ,type: 'date'
        ,range: ['#flight-start-back', '#flight-end-back']
        //,rangeLinked: true // 是否开启日期范围选择时的区间联动标注模式
        //,value: ['2022-05-01', '2022-06-01']
        //,value: '2022-05-01 - 2022-06-01'
    });

    //销售日期
    laydate.render({
        elem: '#sale-date'
        ,type: 'date'
        ,range: ['#sale-start', '#sale-end']
        //,rangeLinked: true // 是否开启日期范围选择时的区间联动标注模式
        //,value: ['2022-05-01', '2022-06-01']
        //,value: '2022-05-01 - 2022-06-01'
    });


    // 订位
    form.on('radio(IsBooking)',function (data){

        if(data.value == "") {

            $('#BookingThreshold').show();
        } else {
            $('#BookingThreshold').hide();
        }
        //alert(data);
    });

    //是否紧张位置过滤
    form.on('radio(IsShortageFilter)',function (data) {

        if(data.value == 'T') {
            $('#ShortageFilterCount').show();
        } else {
            $('#ShortageFilterCount').hide();
        }
    });

    //是否PATA
    form.on('radio(NeedPATA)',function (data) {
        if(data.value == 'T'){
            $('.pata-item').show();
        } else {
            $('.pata-item').hide();
        }
    });

    //是否共享
    form.on('radio(IsShareFlight)',function (data) {

        if(data.value == 'T') {
            $('#IsValidCarrierAirline').show();
        } else {
            $('#IsValidCarrierAirline').hide();
        }

    })

    form.on('radio(IsValidCarrierAirline)',function (data) {

        if(data.value == 'T') {
            $('#CarrierAirlines').show();
        } else {
            $('#CarrierAirlines').hide();
        }
    })

    //单程往返
    form.on('radio(FlightWay)',function (data){
        if(data.value == 'S') {
            $('.go-item').show();
            $('.back-item').hide();
        } else {
            $('.go-item').hide();
            $('.back-item').show();
        }
    })

    /**
     * 去程班期
     */
    $('#flight-week-all').on('click',function () {

        $('input[name="Days[]"]').each(function () {
            this.checked = true;
        });

        form.render('checkbox'); //刷新
    });

    $('#flight-week-invert').on('click',function () {

        $('input[name="Days[]"]').each(function () {
            this.checked = !this.checked;
        });

        form.render('checkbox'); //刷新
    })

    /**
     * 返程班期
     */
    $('#flight-week-all-back').on('click',function () {

        $('input[name="BackDays[]"]').each(function () {
            this.checked = true;
        });

        form.render('checkbox'); //刷新
    });

    $('#flight-week-invert-back').on('click',function () {

        $('input[name="BackDays[]"]').each(function () {
            this.checked = !this.checked;
        });

        form.render('checkbox'); //刷新
    })

    /**
     * 销售周期
     */
    $('#sale-week-all').on('click',function () {

        $('input[name="SaleDays[]"]').each(function () {
            this.checked = true;
        });

        form.render('checkbox'); //刷新
    });

    $('#sale-week-invert').on('click',function () {

        $('input[name="SaleDays[]"]').each(function () {
            this.checked = !this.checked;
        });

        form.render('checkbox'); //刷新
    })

    //乘客人数
    form.on('checkbox(HideSales)',function (data){

        if(data.elem.checked == true) {

            $('#passengers-num').show();
        } else {
            $('#passengers-num').hide();
        }
        //alert(data);
    });

    //身份证限制
    form.on('radio(IDLimit)',function (data){

        if(data.value == 'T') {

            $('#IDCardList').show();
        } else {
            $('#IDCardList').hide();
        }
        //alert(data);
    });

    //库存类型
    form.on('radio(InventoryType)',function (data) {
        if(data.value == 'FIX') {

            $('.IsReturnRefundInventory').show();
        } else {
            $('.IsReturnRefundInventory').hide();
        }

        form.render();
    })

    //竞价空间
    form.on('radio(BidType)',function (data) {
        if(data.value == 0) {

            $('.BiddingSpace_Money').show();
            $('.BiddingSpace_Percentage').hide();
        } else {
            $('.BiddingSpace_Money').hide();
            $('.BiddingSpace_Percentage').show();
        }
    })

    //库存类型
    $("#TicketInventory").bind('blur', function(event) {
        var inputData = $("#TicketInventory").val();
        $('input[name="LeftInventory"]').val(inputData);
    });

    //可售航班
    var flightSaleAddFunction = function (flightObj,SaleLimitHtml) {

        if (flightObj.css("display") === "none"){

            flightObj.show();
        }

        flightObj.append(SaleLimitHtml);
        flightSaleFunction();
    }

    //航班
    var FlightSaleHtml = $('#SaleFlightHtml').html();

    //单程航班销售
    var saleSFlightCount = function () {
        return $('#FlightSale .layui-inline').length +  $('#SaleLimit .layui-inline').length;
    }

    $('#SaleFlightBtn').on('click',function () {
        var FlightSale = $('#FlightSale');
        var index = saleSFlightCount();
        var context = {saleFlight:"SaleFlightList",index: index,IsCanSale:'T'};
        flightSaleAddFunction(FlightSale,$.nano(FlightSaleHtml,context));
    });

    //禁售航班
    $('#SaleLimitBtn').on('click',function () {
        var SaleLimit = $('#SaleLimit');
        var index = saleSFlightCount();
        var context = {saleFlight:"SaleFlightList",index: index,IsCanSale:'F'};
        flightSaleAddFunction(SaleLimit,$.nano(FlightSaleHtml,context));
    });

    //往返可售航班
    var saleDFlightCount = function () {
        return $('#BackFlightSale .layui-inline').length +  $('#BackSaleLimit .layui-inline').length;
    }
    $('#BackSaleFlightBtn').on('click',function () {
        var BackFlightSale = $('#BackFlightSale');
        var index = saleDFlightCount();
        var context = {saleFlight:"BackSaleFlightList",index: index,IsCanSale:'T'};
        flightSaleAddFunction(BackFlightSale,$.nano(FlightSaleHtml,context));
    });

    //往返禁售航班
    var BackSaleLimit = $('#BackSaleLimit');
    $('#BackSaleLimitBtn').on('click',function () {
        var index = saleDFlightCount();
        var context = {saleFlight:"BackSaleFlightList",index: index,IsCanSale:'F'};
        flightSaleAddFunction(BackSaleLimit,$.nano(FlightSaleHtml,context));
    });

    //删除航班规则
    var flightSaleFunction = function () {

        //点击删除
        $('.FlightSaleDelete').on('click',function () {
            $(this).parent().parent().remove();
        })
    }
    flightSaleFunction();



    //添加运价信息
    var piecesFun = function (){

        return $('#yun-jia .piece').length;
    }

    var yunJiaHtml = $('#yun-jia-html').html();

    $('#yun-jia-add').on('click',function () {
        yunJiaOnclick();
    });

    var deleteYunJia = function (clickType) {

        var yunJiaObj = null;

        var pieceIndex = 0;
        $('#yun-jia .piece').each(function() {
            pieceIndex++;
            $(this).find('.yun-jia-index').html('第'+pieceIndex+'条运价信息')
            if (pieceIndex === piecesFun()) {

                if (clickType === "ADD") {

                    $(this).find('.yun-jia-delete').on('click',function () {

                        if (piecesFun() === 1) {

                            layer.msg('至少有1条运价信息', {
                                offset: '15px'
                                ,icon:   5
                                ,time: 3000
                            }, function(){});

                            return;
                        }
                        deleteYunJia("DELETE");
                    });
                } else {

                    $(this).remove();
                }

            }

        });
    };

    var yunJiaOnclick = function () {
        var context = {piece:piecesFun()};
        $('#yun-jia').append($.nano(yunJiaHtml,context));
        deleteYunJia("ADD");
        form.render();
    }

    if (piecesFun() === 0) {

        yunJiaOnclick();
    } else {

        deleteYunJia("ADD");
    }



})